<template>
	<div class="my-goods-list">
		<MyTable :arr="list">
			<template v-slot:head>
				<th>#</th>
				<th>商品名称</th>
				<th>价格</th>
				<th>标签</th>
				<th>操作</th>
			</template>
			<!-- scoped结果里面是{obj：item} -->
			<template #body="scoped">
				<td>{{ scoped.obj.id }}</td>
				<td>{{ scoped.obj.goods_name }}</td>
				<td>{{ scoped.obj.goods_price }}</td>
				<td>
					<div>
						<!-- 输入框 -->
						<input style="width: 150px" class="tag-input form-control" type="text" v-if="scoped.obj.inputVisible" v-model="scoped.obj.inputValue" v-focus @blur="scoped.obj.inputVisible = false" @keyup.esc="scoped.obj.inputValue = '' " @keyup.enter="addFn(scoped.obj)"/>
						<!-- 将输入框的内容，添加到tags数组里面去 -->
						<button v-else class="btn btn-primary btn-sm add-tag" @click="scoped.obj.inputVisible = true">
							+Tag
						</button>
					</div>
					<span class="badge bg-warning text-dark" v-for="(item,index) in scoped.obj.tags " :key="index">{{ item}}</span>
				</td>
				<td><button class="btn btn-danger btn-sm" @click="delFn(scoped.obj.id)">删除</button></td>
			</template>
		</MyTable>
	</div>
</template>

<script>
	// 接口：GTE  https://www.escook.cn/api/goods
import MyTable from '@/components/MyTable.vue';
import axios from 'axios';

	export default {
		data() {
			return {
				list:[]
			};
		},
		components:{
			MyTable
		},
		async created(){
			// 发送请求
		    const {data:res} = await axios.get('https://www.escook.cn/api/goods')
			console.log(res);
			this.list = res.data
		},
		methods:{
			delFn(id){
				this.list = this.list.filter(item => item.id !== id)
			},
			addFn(obj){
				// 判断非空
				if(obj.inputValue.trim() === ''){
					return 
				}
				obj.tags.push(obj.inputValue)
				obj.inputValue = ''
			}
		},
		// 自定义指令
		directives:{
			focus:{
				inserted(el){
					el.focus()
				}
			}
		}
	};
</script>

<style>
</style>
